package com.yc.d0422;

import com.yc.util.DBHelper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class FileDownload {

    public static void main(String[] args) throws IOException {
       // captureJobs();
        downHtml("http://www.hyycinfo.com/index.html");
    }

    public static String fileDir = "D:/study/z/yc/";

    public static void downHtml(String address) throws IOException {
        URL url = new URL(address);
        String filename = url.getFile();
        String filepath = fileDir + filename;
        download(address, filepath);
        /**
         * 解析页面资源：
         * css=><link href="....">,
         * js => <script src="....",
         * 图片 =><img src=".....">
         */

        final Document doc = Jsoup.parse(new File(filepath), "utf-8");
//        doc.select() ==> 相当于js的document.querySelectorAll();
//        doc.selectFirst() ==> 相当于js的 document.querySelector();
        Elements css = doc.select("link[rel=stylesheet]");
        css.forEach(element -> {
            //获取属性值
            String href = element.attr("href");
            //查找最后一个
            int i = address.lastIndexOf("/");
            String webpath = address.substring(0, i + 1) + href;
            String diskpath = fileDir + href;
            try {
                download(webpath, diskpath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

        Elements js = doc.select("script[src]");
        js.forEach(element -> {
            //获取属性值
            String src = element.attr("src");
            //查找最后一个
            int i = address.lastIndexOf("/");
            String webpath = address.substring(0, i + 1) + src;
            String diskpath = fileDir + src;
            try {
                download(webpath, diskpath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

        Elements img = doc.select("img[src]");
        img.forEach(element -> {
            //获取属性值
            String src = element.attr("src");
            //查找最后一个
            int i = address.lastIndexOf("/");
            String webpath = address.substring(0, i + 1) + src;
            String diskpath = fileDir + src;
            try {
                download(webpath, diskpath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

    }

    public static void download(String address, String filepath) throws IOException {
        System.out.println("开始下载文件");
        System.out.println("address = " + address);
        System.out.println("filepath = " + filepath);

        File file = new File(filepath);
        //获取文件所在文件夹
        final File parentFile = file.getParentFile();
        if (parentFile.exists() == false) {
            //创建文件夹啊
            parentFile.mkdirs();
        }

        URL url = new URL(address);
        URLConnection conn = url.openConnection();

        try (final InputStream in = conn.getInputStream();
             FileOutputStream fos = new FileOutputStream(filepath)) {
            byte[] bytes = new byte[1024];
            int count;
            while ((count = in.read(bytes)) > -1) {
                //输出到文件
                fos.write(bytes, 0, count);
            }
        }
        System.out.println("下载文件结束");
    }

    public static void captureJobs() throws IOException {
        String address = "http://www.07347.com/jobs/jobs_list.html";
        Document doc = Jsoup.parse(new URL(address), 5000);
        Elements elements = doc.select(".J_jobsList");
        elements.forEach(element ->{
            Element a = element.selectFirst("a");
            String href = a.attr("href");
            String webpath = "http://www.07347.com" + href;
            try {
                captureJob(webpath);
            } catch (IOException e) {
                e.printStackTrace();
            }

        });
    }

    public static void captureJob(String address) throws IOException {
//        String address = "http://www.07347.com/jobs/409.html";

        Document doc = Jsoup.parse(new URL(address), 5000);

        String id = address.replaceAll(".+/(\\d+)\\.html", "$1");
        //text() ==> innerText
        String name = doc.selectFirst(".j-n-txt").text();
        Elements elements = doc.select(".itemli");
        String type = elements.get(0).text();
        String nums = elements.get(1).text();
        String dept = elements.get(2).text();
        String edu = elements.get(3).text();
        String works = elements.get(4).text();
        String ages = elements.get(5).text();
        String location = doc.select(".add").text().replace("工作地点", "");
        String sql = "insert into jobs values(?,?,?,?,?,?,?,?,?)";
        DBHelper.update(sql, id, name, type, nums, dept, edu, works, ages, location);
    }
}
